//
//  SaoViewController.swift
//  33web
//
//  Created by apple on 2025/7/29.
//

import UIKit
import swiftScan

class SaoViewController: UIViewController {
    
    var listResult = NSMutableArray()
    var saomaButton: UIButton = {
        let v = UIButton()
        v.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
        v.setTitleColor(UIColor.init(hex: 0xffffff), for: .normal)
        v.setBackgroundImage(UIImage(named: "scan"), for: .normal)
        //        v.setTitle("扫码", for: .normal)
        return v
    }()
    
    var tableview = UITableView()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
        self.tableview = UITableView()
        //        self.tableview.separatorStyle = .none
        self.tableview.delegate = self
        self.tableview.dataSource = self
        self.tableview.register(UITableViewCell.self, forCellReuseIdentifier: "basicCell")
        self.view.addSubview(self.tableview)
        
        self.tableview.snp.makeConstraints { make in
            make.left.right.top.bottom.equalToSuperview()
        }
        
        self.view.addSubview(saomaButton)
        saomaButton.addTarget(self, action:  #selector(sama), for: .touchUpInside)
        saomaButton.snp.makeConstraints { make in
            make.bottom.equalToSuperview().offset(-64)
            make.centerX.equalToSuperview()
            make.width.height.equalTo(50)
            
        }
        
        // Do any additional setup after loading the view.
    }
    
    @objc  func sama() {
        // 创建扫码界面（默认类型是 .all，可扫码和条形码）
        let scanVC = ScanManager.shared.createScanViewController(
            type: .all,  // 只扫描二维码
            result: { [weak self] result in
                // 扫码成功回调
                self?.handleScanResult(result)
            },
            cancel: { [weak self] in
                // 用户取消扫码
                self?.dismiss(animated: true)
                print("Scan cancelled")
            }
        )
        
        // 以模态方式呈现（如果是导航控制器，可以用 pushViewController）
        let navController = UINavigationController(rootViewController: scanVC)
        navController.modalPresentationStyle = .fullScreen
        present(navController, animated: true)
    }
    
    @objc func tiaoma(){
        // 创建扫码界面（默认类型是 .all，可扫码和条形码）
        let scanVC = ScanManager.shared.createScanViewController(
            type: .all,  // 只扫描二维码
            result: { [weak self] result in
                // 扫码成功回调
                self?.handleScanResult(result)
            },
            cancel: { [weak self] in
                // 用户取消扫码
                self?.dismiss(animated: true)
                print("Scan cancelled")
            }
        )
        
        // 以模态方式呈现（如果是导航控制器，可以用 pushViewController）
        let navController = UINavigationController(rootViewController: scanVC)
        present(navController, animated: true)
        
    }
    private func handleScanResult(_ result: String) {
        print("结果为: \(result)")
        dismiss(animated: true) {
            self.listResult.add(result)
            self.tableview.reloadData()
            // 显示结果
            //             let alert = UIAlertController(
            //                 title: "Scan Result",
            //                 message: result,
            //                 preferredStyle: .alert
            //             )
            //             alert.addAction(UIAlertAction(title: "OK", style: .default))
            //             self.present(alert, animated: true)
        }
    }
}
extension SaoViewController:UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.listResult.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "basicCell", for: indexPath)
        cell.textLabel?.text = "\(self.listResult[indexPath.row])"
        return cell
    }
    
    

}
